<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="活动名称">
        <el-input v-model="form.title"></el-input>
      </el-form-item>

      <!-- 富文本框内容 -->
      <el-form-item label="内容" class="content">
        <VueEditor v-model="form.content" />
      </el-form-item>

      <!-- 复选框 -->
      <el-form-item label="分类">
        <el-checkbox-group v-model="checkList">
          <el-checkbox
            :label="item.id"
            v-for="item in categoryList"
            :key="item.id"
          >
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <!-- 图片上传 -->
      <el-form-item>
        <el-upload
          :file-list="form.cover"
          :action="$axios.defaults.baseURL + '/upload'"
          list-type="picture-card"
          :headers="{
            Authorization: token,
          }"
          :on-success="coverSuccess"
          :on-remove="coverRemove"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>

      <!-- 单选框 -->
      <el-form-item label="类型">
        <el-radio-group v-model="form.type">
          <el-radio :label="1">文章</el-radio>
          <el-radio :label="2">视频</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { VueEditor } from "vue2-editor";
export default {
  data() {
    return {
      token: localStorage.getItem("token"),
      content: "",
      form: {
        title: "",
        content: "",
        categories: [],
        cover: [],
        type: 1,
      },
      checkList: [],
      categoryList: [],
    };
  },
  components: {
    VueEditor,
  },
  watch: {
    checkList() {
      // 每当checkList变化的时候，把它映射成请求数据需要的{id:对应的数字}
      this.form.categories = this.checkList.map((num) => {
        return {
          id: num,
        };
      });
    },
  },
  methods: {
    coverSuccess(res, file, fileList) {
      // console.log("上传成功");
      // console.log(res);
      // 把图片的id存到cover中，以api的格式发送请求
      const id = res.data.id;
      // this.form.cover.push({
      //   id,
      // });
      //  现在这里的成功上传推送进 form.cover 的数据不仅仅
      // 用来发送 api 请求, 也会用来渲染上传组件, 单单一个 id 就不够
      // 还要有这个 file 用来显示
      file.id = id;
      this.form.cover.push(file);
    },
    coverRemove(file, fileList) {
      console.log("文件已删除");
    },
    onSubmit() {
      console.log(this.form);
      this.$axios({
        url: "/post",
        method: "post",
        data: this.form,
      }).then((res) => {
        // console.log(res);
        if (res.data.message == "文章发布成功") {
          this.$router.push("/postlist");
        }
      });
    },
  },
  created() {
    this.$axios({
      url: "/category",
    }).then((res) => {
      // console.log(res.data);
      // 把头条和热点栏目删除
      res.data.data.splice(0, 2);
      this.categoryList = res.data.data;
    });
    // 如果浏览器里面带有id值，则需要通过id来获取要编辑的文章
    if (this.$route.query.id) {
      this.$axios({
        url: "/post/" + this.$route.query.id,
      }).then((res) => {
        // 处理富文本框无法接受 div 的问题
        // console.log(res.data);
        // 把所有带有div的标签换成p标签
        res.data.data.content = res.data.data.content.replace(/div/g, "p");
        // 处理分类选项多选框需要的数字数组
        // [{id: 666}] => [666]
        this.checkList = res.data.data.categories.map((item) => {
          return item.id;
        });
        // 处理封面图片的数据
        res.data.data.cover.map((img) => {
          if (img.url.indexOf("http") == -1) {
            img.url = this.$axios.defaults.baseURL + img.url;
          }
          img.uid = img.id;
          return img;
        });
        this.form = res.data.data;
      });
    }
  },
};
</script>

.<style lang="less" scoped>
.content {
  /deep/ .el-form-item__content {
    line-height: 1;
  }
}
</style>